from loguru import logger
from fastapi import APIRouter,Depends
from model.equipment import Equipment,Item,UpdateInfo
from sqlalchemy.orm import Session
from db.mysql import get_db
from vendor.extend.conversion import is_positive_integer
from sqlalchemy import desc
from vendor.extend.courier import *
Configuration = APIRouter()

@Configuration.post('/settings')
async def settings(item:Item, db: Session = Depends(get_db)):
    # 解析JSON数据
    item_dict = item.dict()
    equipment=Equipment(
            number= item_dict.get('number',''),
            work_face_width= item_dict.get('work_face_width',0),
            work_face_height=item_dict.get('work_face_height',0),
            work_face_distance=item_dict.get('work_face_distance',0),
            vertical_threshold=item_dict.get('vertical_threshold',0),
            coal_thickness= item_dict.get('coal_thickness',0),
            create_time=int(time.time())
    )

    try:
        # 执行一些数据库操作
        db.add(equipment)
        db.flush()
        db.refresh(equipment)
        db.commit()
    except Exception as e:
        # 手动回滚会话并重新抛出异常
        db.rollback()
        logger.error("工作面信息 添加 出错:{}", e)


    return Success(data={'equipment_id':equipment.id,'create_time':equipment.create_time})

@Configuration.post('/update')
async def update(item:UpdateInfo, db: Session = Depends(get_db)):
    item_dict = item.dict()
    equipment={
        'number': item_dict.get('number',''),
        'work_face_width': item_dict.get('work_face_width',0),
        'work_face_height':item_dict.get('work_face_height',0),
        'work_face_distance':item_dict.get('work_face_distance',0),
        'vertical_threshold':item_dict.get('vertical_threshold',0),
        'coal_thickness':item_dict.get('coal_thickness',0),
        'update_time':int(time.time())
    }

    try:
        # 执行一些数据库操作
        db.query(Equipment).filter_by(id=item_dict.get('equipment_id',0)).update(equipment)
        db.commit()
    except Exception as e:
        # 手动回滚会话并重新抛出异常
        db.rollback()
        logger.error("工作面信息 更新 出错:{}", e)

    return Success(data=equipment)

@Configuration.get('/list')
async def getlist(db: Session = Depends(get_db)):
    list_data=db.query(Equipment).order_by(desc("id")).all()
    user_list = [{
        "id":info.id,
        "number":info.number,
        "work_face_width":info.work_face_width,
        "work_face_height":info.work_face_height,
        "work_face_distance":info.work_face_distance,
        "vertical_threshold":info.vertical_threshold,
        "coal_thickness":info.coal_thickness,
        "update_time":info.update_time,
        "create_time":info.create_time
    } for info in list_data]  # 转换为字典列表
    return Success(data=user_list)


@Configuration.get('/dropdown')
async def dropdown(db: Session = Depends(get_db)):
    list_data=db.query(Equipment.id,Equipment.number).order_by(Equipment.id.desc()).all()
    data_list=[]
    for info in list_data:
        data_list.append({
            'id':info.id,
            'number':info.number
        })

    return Success(data=data_list)

@Configuration.get('/info')
async def getinfo(equipment_id:int,db: Session = Depends(get_db)):
    if not is_positive_integer(equipment_id):
        return Error(msg='工作面ID必须为正整数')
    info = db.query(Equipment).filter_by(id=equipment_id).first()
    if info:
        return Success(data={
            "id":info.id,
            "number":info.number,
            "work_face_width":info.work_face_width,
            "work_face_height":info.work_face_height,
            "work_face_distance":info.work_face_distance,
            "vertical_threshold":info.vertical_threshold,
            "coal_thickness":info.coal_thickness,
            "update_time":info.update_time,
            "create_time":info.create_time
        })
    else:
        return Error(msg='未找到工作面相关数据')


